package com.example.myapplication.Fragment;

import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.myapplication.activity.BaseOnclickListener;
import com.example.myapplication.R;
import com.example.myapplication.activity.SongDetail1Activity;
import com.example.myapplication.adapter.recyclerViewAdapter.HotSongAdapter;
import com.example.myapplication.api.MusicApiService;
import com.example.myapplication.entity.Artist;
import com.example.myapplication.entity.BaseResponse;
import com.example.myapplication.entity.HotSongs;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


public class HotSongFragment extends Fragment {

    private RecyclerView recycler_View_hotsong;
    private View view;
    private HotSongAdapter adapter;
    private Artist artist ;
    private List<HotSongs> hotSongs;

    public HotSongFragment() {

    }

    public static HotSongFragment newInstance(Bundle bundle) {
        HotSongFragment fragment = new HotSongFragment();
        fragment.setArguments(bundle);
        return fragment;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_hot_song, container, false);
        //artist = getArguments().getParcelable("singer");
        artist = (Artist) getArguments().getSerializable("singer");
        Log.e("id: ", String.valueOf(artist.getId()));
        initRecyclerView();

        adapter.setBaseOnclickListener(new BaseOnclickListener() {
            @Override
            public void onclick(int position) {
                Intent intent = new Intent(getContext(), SongDetail1Activity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("song",hotSongs.get(position));
                intent.putExtras(bundle);
                startActivity(intent);

            }
        });
        return view;
    }

    private void initRecyclerView() {
        recycler_View_hotsong = view.findViewById(R.id.recycler_view_hotsong);
        RecyclerView.LayoutManager manager = new LinearLayoutManager(getContext());
        recycler_View_hotsong.setLayoutManager(manager);
        adapter = new HotSongAdapter();
        recycler_View_hotsong.setAdapter(adapter);
        loadData();
    }


    private void loadData() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://autumnfish.cn/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        MusicApiService service =  retrofit.create(MusicApiService.class);

        Call<BaseResponse> call = service.getHotSong(artist.getId());
        call.enqueue(new Callback<BaseResponse>() {
            @Override
            public void onResponse(Call<BaseResponse> call, Response<BaseResponse> response) {
                BaseResponse baseResponse = response.body();
                //Log.e("onResponse: ", String.valueOf(baseResponse.getCode()));
                hotSongs = baseResponse.getsongs();
                adapter.setData(hotSongs);
            }
            @Override
            public void onFailure(Call<BaseResponse> call, Throwable t) {

                t.printStackTrace();
            }
        });
    }
}